﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;

namespace ProcessesSMS
{
	/// <summary>
	/// Summary description for.
	/// Project name: SMS_xoso
	/// channel: n/a
	/// command code: HN X Y
	/// created: 01/09/09 .
	/// Writer : Nampt + Tuanbc
	/// Company: VIT TELECOM
	/// </summary>

	public class XOSO_DD
	{
		#region Declare
		private SMS_MO WMO;
		private SMS_MT WMT;
		ArrayList arr = new ArrayList();
		public XOSO_DD(SMS_MO MO)
		{
			WMO = MO;
		}

		public XOSO_DD()
		{
		}
		#endregion
		public void run()
		{
			WMT = new SMS_MT(WMO);
			string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
			tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();
			string[] msg = tmpMsg.Split(new char[] { ' ' });

			string strReturnMsg = "";   
			string supertemp="";
			strReturnMsg = GetReturnMsg(msg);
			int tempvalue = strReturnMsg.Length;
			supertemp = strReturnMsg;
			if ((tempvalue >0) && (tempvalue <160))
			{		
				// 1 mess
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;

				WMT.Insert(); 
			}
			else if ((tempvalue >=160) && (tempvalue <320))
			{
				// 2 mess
				strReturnMsg = supertemp.Substring(0,160);
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
				
				strReturnMsg = supertemp.Substring(160);
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;

				WMT.Insert(); 				
			}
			else if ((tempvalue >=320) && (tempvalue <480))
			{
				// 3 mess
				strReturnMsg = supertemp.Substring(0,160);
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
				
				strReturnMsg = supertemp.Substring(160,160);
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;

				strReturnMsg = supertemp.Substring(320);
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;

				WMT.Insert(); 			
			}
			else
			{
				// 4 mess
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
				
				strReturnMsg = supertemp.Substring(160,160);
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;

				strReturnMsg = supertemp.Substring(320,160);
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;

				strReturnMsg = supertemp.Substring(480);
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;

				WMT.Insert(); 			
			}
			return;          
		}
		public string GetReturnMsg(string[] arr)
		{
			string msgBody= string.Empty;
			string strSQL = "";
			if(WMO.ServiceID.Replace("04","")=="6572" || WMO.ServiceID.Replace("04","")=="6772" || WMO.UserID=="84984696925")
			{
				string connStr = ConfigurationSettings.AppSettings["STOCK_CONNECTION_STRING"].ToString();
				if (arr[0]=="TD" || arr[0]=="HN" || arr[0]=="QN" || arr[0]=="BN" || arr[0]=="HP" || arr[0]=="ND" || arr[0]=="TB" || arr[0]=="")
				{
					 strSQL = "SELECT TOP 1 * FROM XOSO_DD WHERE MaMien='MB'";
				}	
				else
				{
					 strSQL = "SELECT TOP 1 * FROM XOSO_DD WHERE MaTinh='" + arr[0] + "'";
				}
				SqlConnection conn = new SqlConnection(connStr);
				conn.Open();
				SqlDataAdapter da = new SqlDataAdapter(strSQL,conn);
				DataSet ds = new DataSet("XOSO");
				da.Fill(ds);
				DataTable dt = ds.Tables[0];
				if (dt.Rows.Count >0)
				{
					msgBody = "DU DOAN SO PHAT TAI\n" + "Nhieu 30ngay: " + dt.Rows[0]["ManyTimes30"] + "\nNhieu 120ngay: " + dt.Rows[0]["ManyTimes120"].ToString();
					msgBody += "\nIt 30ngay: " + dt.Rows[0]["LessTimes30"].ToString() + "\nIt 120ngay: " + dt.Rows[0]["LessTimes120"].ToString();
					msgBody += "\nDU DOAN: " + dt.Rows[0]["GuessNumber"].ToString();
					msgBody = msgBody.Replace("lần", "lan");
				}
				else
				{
					msgBody="De du doan so phat tai cung 6x72 soan DD Matinh gui 6572. Vi du: De du doan kqxs mien bac soan DD TD gui 6572. Chuc may man !";
				}
			}
			else
			{
				msgBody="De du doan so phat tai cung 6x72 soan DD Matinh gui 6572. Vi du: De du doan kqxs mien bac soan DD TD gui 6572. Chuc may man !";
			}
			return msgBody;
		}
	}		
}
